package com.scalar.chiptrack.adaptor.beceem;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.FabSortLot;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.TestLotBinReport;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.ShippedLot;
import com.scalar.chiptrack.adaptor.utils.ParserResult;
import com.scalar.chiptrack.adaptor.utils.Parser;
import com.scalar.chiptrack.adaptor.utils.ParserFactory;
import com.scalar.chiptrack.adaptor.utils.CSVParserImpl;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.FabSortAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.ShippedLotDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.utils.DateUtil;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Date;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 29 Mar, 2010
 * Time: 12:21:40 PM
 */
public class ASEDataParser extends JobHandler implements AdaptorConstants
{
    private HashMap<String, InventoryLot> m_waferBankDataMap    = new HashMap<String, InventoryLot>();
    private HashMap<String, FabSortLot> m_sortDataMap           = new HashMap<String, FabSortLot>();
    private HashMap<String, ShippedLot> m_sortShipDataMap       = new HashMap<String, ShippedLot>();
    private HashMap<String, FabSortLot> m_sortBinDataMap        = new HashMap<String, FabSortLot>();
    private HashMap<String, FabSortLot> m_sortTotalMap          = new HashMap<String, FabSortLot>();

    private HashMap<String, InventoryLot> m_dieBankDataMap    = new HashMap<String, InventoryLot>();
    private HashMap<String, AssemblyLot> m_assemblyDataMap    = new HashMap<String, AssemblyLot>();
    private HashMap<String, ShippedLot> m_assyShipDataMap     = new HashMap<String, ShippedLot>();
    private HashMap<String, TestLot> m_ftDataMap              = new HashMap<String, TestLot>();
    private HashMap<String, InventoryLot> m_fgDataMap         = new HashMap<String, InventoryLot>();
    private HashMap<String, ShippedLot> m_testShipDataMap     = new HashMap<String, ShippedLot>();


    private Logger m_parserLogger   = null;
    private String m_dataOutputDir  = null;


    public ASEDataParser() throws ScalarException
    {
        try
        {
            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_ASEK, LOCATION_TYPE_ASSEMBLY);
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_ASEK );

            TestAdaptorDAO.setLogger( m_parserLogger );
            ShippedLotDAO.setLogger( m_parserLogger );
            AssemblyAdaptorDAO.setLogger(m_parserLogger);
            InventoryLotDAO.setLogger( m_parserLogger);
            AdaptorUtils.setLogger( m_parserLogger );
        }
        catch (ScalarException se)
        {
            log("ERROR!! ERROR!! ERROR!! Exception while loading ASE WebSite output directory details from database, Exception: " + se);
            throw se;
        }
    }
    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main( String args[] ) throws Exception
    {
        try
        {
            new ASEDataParser().execute();
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    /**
     *
     * @throws Exception
     */
    public void execute() throws Exception
    {
        AdaptorUtils.printHeader(VENDOR_NAME_ASEK + " " + VENDOR_NAME_ASET, CONSTANT_PARSER);

        File doneDestDir = new File(  m_dataOutputDir + "/done" );

        File sourceDataoutDir = new File( m_dataOutputDir );

        File[] fileList = sourceDataoutDir.listFiles();
        if ( ( fileList == null ) || ( fileList.length <= 3 ) )
        {
            log( "No file found in directory: " + sourceDataoutDir );
            throw new ScalarException( "\n+-----------No file found in directory " + sourceDataoutDir );
        }

        ArrayList<File> successFileList = new ArrayList<File>();

        boolean success = false;

        for ( int i = 0; i < fileList.length; i++ )
        {
            if ( fileList[i].isDirectory() == true )
            {
                continue;
            }

            try
            {
                // Step 1
                // Parse Files to get Object List
                if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith( ASEAdaptor.FILE_NAME_TURNKEY_WIP ) )
                {
                    parseTurnKeyWipReport( fileList[i].getAbsolutePath() );

                    successFileList.add( fileList[i] );
                }
                if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith( ASEAdaptor.FILE_NAME_ASSY_SHIP ) )
                {
                    parseAssyShipReport( fileList[i].getAbsolutePath() );

                    successFileList.add( fileList[i] );
                }
                if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith( ASEAdaptor.FILE_NAME_FT_SHIP ) )
                {
                    parseTestShipReport( fileList[i].getAbsolutePath() );

                    successFileList.add( fileList[i] );
                }
                /*if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith( ASEAdaptor.FILE_NAME_ASSY_YIELD) )
                {
                    parseAssyYieldReport( fileList[i].getAbsolutePath() );

                    successFileList.add( fileList[i] );
                }*/

                if ( fileList[i].getName() != null && fileList[i].getName().trim().startsWith( ASEAdaptor.FILE_NAME_FT_YIELD ) )
                {
                    ArrayList<TestLotBinReport> testBinList = parseFTBinReport( fileList[i].getAbsolutePath() );
                    if ( testBinList != null && testBinList.size() > 0 )
                    {
                        //to get the ASECL Test Bin Details
                        if ( testBinList.isEmpty() == false )
                        {
                            log( "Updating ASET FT Binning Information" );
                            TestAdaptorDAO.saveTestBinInfo ( null, testBinList , VENDOR_NAME_ASET );
                            success = true;
                        }
                        else
                        {
                            log( "ASET FT Binning Information List is Empty" );
                        }
                    }
                    successFileList.add( fileList[i] );
                }

                success = true;
            }
            catch ( Exception e )
            {
                success = false;
                log( "Exception while parsing and storing file: " + fileList[i].getAbsolutePath() + "\nException: " + e );
                e.printStackTrace();
                throw e;
            }
        }
        /*Updating the Wafer Store Lot Information*/
        try
        {
            // Save Wafer Store Information
            if (m_waferBankDataMap.isEmpty() == false)
            {
                log(" Updating Wafer Store Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_waferBankDataMap.values().iterator(), LOCATION_TYPE_WAFER_BANK, VENDOR_NAME_ASET);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_WAFER_BANK, VENDOR_NAME_ASET);
            }
            success = true;
            m_waferBankDataMap.clear();
            m_waferBankDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Wafer Store Lot information into database: " + se);
            throw se;
        }

        try
        {
            if ( m_sortBinDataMap.isEmpty() == false )
            {
                FabSortAdaptorDAO.saveSortBinData( null, m_sortBinDataMap.values().iterator() );
            }
            success = true;
            m_sortBinDataMap.clear();
        }
        catch(Exception e)
        {
            success = false;
            System.out.println(" Exception Occrred While Saving the Sort Binning Info " +e );
            throw e;
        }
        //to saving sort Lot Info
        try
        {
            // Save Sort Information
            if ( m_sortDataMap.isEmpty() == false )
            {
                log( " Updating Sort Lot Information" );
                FabSortAdaptorDAO.saveSortLot( null, m_sortDataMap.values().iterator(), WIP_TYPE_SORT, VENDOR_NAME_ASET );
            }
            else
            {
                FabSortAdaptorDAO.closeFabSortLot( null, WIP_TYPE_SORT, VENDOR_NAME_ASET );
            }
            success = true;
            m_sortDataMap.clear();
            m_sortDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Sort Lot information into database: " + se );
            throw se;
        }

        try
        {
            // Save Sort Information
            if ( m_sortShipDataMap.isEmpty() == false )
            {
                log( " Updating Sort Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_sortShipDataMap.values().iterator() );
            }
            success = true;
            m_sortShipDataMap.clear();
            m_sortShipDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Sort Shipping Lot information into database: " + se );
            throw se;
        }
         /*Updating the Die Bank Lot Information*/
        try
        {
            // Save Die Bank Information
            if (m_dieBankDataMap.isEmpty() == false)
            {
                log(" Updating Die Bank Lot Information");
                InventoryLotDAO.saveInventoryLot(null, m_dieBankDataMap.values().iterator(), LOCATION_TYPE_DIE_BANK, VENDOR_NAME_ASEK);
            }
            else
            {
                InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_DIE_BANK, VENDOR_NAME_ASEK);
            }
            success = true;
            m_dieBankDataMap.clear();
            m_dieBankDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log("ERROR!! ERROR!! ERROR!! Could not update Die Bank Lot information into database: " + se);
            throw se;
        }
        /*Assembly Lot Saving*/
        try
        {
            // Save Assembly Information
            if ( m_assemblyDataMap.isEmpty() == false )
            {
                log( " Updating Assembly Lot Information" );
                AssemblyAdaptorDAO.saveAssemblyLot( null, m_assemblyDataMap.values().iterator(),WIP_TYPE_ASSEMBLY, VENDOR_NAME_ASEK );
            }
            else
            {
                AssemblyAdaptorDAO.closeAssemblyLot( null, WIP_TYPE_ASSEMBLY, VENDOR_NAME_ASEK);
            }
            success = true;
            m_assemblyDataMap.clear();
            m_assemblyDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se );
            throw se;
        }
        // Save Assembly Ship Information
        try
        {
            if ( m_assyShipDataMap.isEmpty() == false )
            {
                log( " Updating Assembly Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_assyShipDataMap.values().iterator() );
            }
            success = true;
            m_assyShipDataMap.clear();
            m_assyShipDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Assembly Shipping Lot information into database: " + se );
            throw se;
        }
        // Saving the Test Lot Information
        try
        {
            // Save Final Test Information
            if ( m_ftDataMap.isEmpty() == false )
            {

                log( " Updating Test Lot Information" );
                TestAdaptorDAO.saveTestLot( null, m_ftDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_ASET );
            }
            else
            {
                TestAdaptorDAO.closeTestLot( null, LOCATION_TYPE_FT, VENDOR_NAME_ASET );
            }

            success = true;
            m_ftDataMap.clear();
            m_ftDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se );
            throw se;
        }
        try
        {
            // Save Final Test Information
            if ( m_testShipDataMap.isEmpty() == false )
            {
                log( " Updating Test Shiping Lot Information" );
                ShippedLotDAO.saveShippedLot( null, m_testShipDataMap.values().iterator() );
            }
            success = true;
            m_testShipDataMap.clear();
            m_testShipDataMap = null;
        }
        catch (ScalarException se)
        {
            success = false;
            se.printStackTrace();
            log( "ERROR!! ERROR!! ERROR!! Could not update Test Shipping Lot information into database: " + se );
            throw se;
        }
        // to save the Finished Goods lot tables
       try
       {
           if ( m_fgDataMap.isEmpty() == false )
           {
               log(" Updating Finished Goods Lot Information");
               InventoryLotDAO.saveInventoryLot( null, m_fgDataMap.values().iterator(), LOCATION_TYPE_FG, VENDOR_NAME_ASET );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(null, LOCATION_TYPE_FG, VENDOR_NAME_ASET );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Finished Goods Lot information into database: " + se);
          throw se;
      }

	  AdaptorDAO.closeDropOutLots( VENDOR_NAME_ASET );
  	  AdaptorDAO.closeDropOutLots( VENDOR_NAME_ASEK );
        // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
        AdaptorDAO.sendAllNotifications(VENDOR_NAME_ASET, true, true, true);
        AdaptorDAO.sendAllNotifications(VENDOR_NAME_ASEK, true, true, true);

        int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( ASEAdaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

        // Update Adaptor_Run_Info table
        try
        {
            if ( success )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, ASEAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
        }
        catch ( ScalarException se )
        {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, ASEAdaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
        }
        // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
    }

    private void parseTurnKeyWipReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result     = null;
        AssemblyLot assemblyLot = null;
        TestLot testLot         = null;
        InventoryLot inventoryLot = null;
        FabSortLot fabSortLot   = null;
        String lotNumber        = null;
        String partNumber       = null;
        String fabLotNumber     = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for Turn Key WIP Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for Turn Key WIP Report..." );

             Parser parser = ParserFactory.getCSVParser( detailWipFile, ',' );

            ((CSVParserImpl)parser).setHeaderRowIdx(2);
            ((CSVParserImpl)parser).setDataStartRowIdx(3);

            result = parser.parse();

            while ( result.next() )
            {
                partNumber = result.getString("SOURCE_DEVICE");
                fabLotNumber = result.getString("CUST_LOT");
                lotNumber    = result.getString("ASE_SCH");

                if (lotNumber == null || "-".equalsIgnoreCase( lotNumber ) )
                {
                    lotNumber = fabLotNumber;
                }

                if (partNumber!= null && partNumber.trim().length()>2 && lotNumber != null && lotNumber.trim().length() > 2 )
                {
                    if ( result.getInt("CP_UNISS") > 0 )
                    {
                        log( lotNumber +" In Wafer Bank");

                        inventoryLot = (InventoryLot)m_waferBankDataMap.get( partNumber + fabLotNumber );

                        if ( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot( VENDOR_NAME_ASET, LOCATION_TYPE_WAFER_STORE );
                            m_waferBankDataMap.put( partNumber + fabLotNumber, inventoryLot );
                        }

                        if( inventoryLot.getWaferQtyIn() != null )
                        {
                             try
                             {
                                 inventoryLot.setWaferQtyIn(( Integer.parseInt( inventoryLot.getWaferQtyIn() ) + result.getInt("CP_UNISS") ) + "" );
                             }
                             catch(Exception e)
                             {
                                 inventoryLot.setWaferQtyIn( result.getInt("CP_UNISS") +"" );
                             }
                         }
                         else
                         {
                             inventoryLot.setWaferQtyIn( result.getInt("CP_UNISS")+"" );
                         }

                         if ( inventoryLot.getWaferQtyIn() != null && !"0".equalsIgnoreCase(inventoryLot.getWaferQtyIn() ) )
                         {
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setLotNumber( fabLotNumber );
                            inventoryLot.setFabLotNumber( fabLotNumber );
                            inventoryLot.setStatus( LOT_STATUS_RUNNING );
                            if ( result.getString("LOTTYPE") != null && "Engineering".equalsIgnoreCase(result.getString("LOTTYPE")) )
                            {
                                inventoryLot.setLotCategory( LOT_CATEGORY_TYPE_ENGG );
                            }
                            else
                            {
                                inventoryLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                            }
                        }
                    }

                     //CP
                    if (  result.getInt("CP") > 0 || result.getInt("CP_ELSE") > 0 || result.getInt("CP_INTRA") > 0
                            || result.getInt("CP_HOLD") > 0 || result.getInt("CP_HLDBNK") > 0 || result.getInt("WQ")  > 0 || result.getInt("WP") > 0 )

                   {
                        //log( "In Sort" + lotNumber ) ;

                        fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fabLotNumber  );
                        if (fabSortLot == null)
                        {
                            fabSortLot = new FabSortLot(VENDOR_NAME_ASET,WIP_TYPE_SORT );
                            m_sortDataMap.put( partNumber + fabLotNumber , fabSortLot);
                        }

                        // FOR SETTING THE QTY IN
                        if( result.getInt("CP") > 0 )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fabLotNumber  );
                           if( fabSortLot.getQtyIn() != null )
                           {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + result.getInt("CP") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( result.getInt("CP")+"" );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( result.getInt("CP")+"" );
                            }
                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", CP") ;
                            }
                            else
                            {
                                fabSortLot.setStage("CP");
                            }
                        }
                        if( result.getInt("CP_ELSE") > 0 )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fabLotNumber  );
                           if( fabSortLot.getQtyIn() != null )
                           {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + result.getInt("CP_ELSE") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( result.getInt("CP_ELSE") +"" );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( result.getInt("CP_ELSE") +"" );
                            }
                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", CP_ELSE") ;
                            }
                            else
                            {
                                fabSortLot.setStage("CP_ELSE");
                            }
                        }
                        if( result.getInt("CP_INTRA") > 0 )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fabLotNumber  );
                           if( fabSortLot.getQtyIn() != null )
                           {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + result.getInt("CP_INTRA") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( result.getInt("CP_INTRA")+"" );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( result.getInt("CP_INTRA")+"" );
                            }
                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", CP_INTRA") ;
                            }
                            else
                            {
                                fabSortLot.setStage("CP_INTRA");
                            }
                        }
                        if( result.getInt("CP_HOLD") > 0 )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fabLotNumber  );
                           if( fabSortLot.getQtyIn() != null )
                           {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + result.getInt("CP_HOLD") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( result.getInt("CP_HOLD")+"" );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( result.getInt("CP_HOLD")+"" );
                            }
                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", CP_HOLD") ;
                            }
                            else
                            {
                                fabSortLot.setStage("CP_HOLD");
                            }
                        }
                        if( result.getInt("CP_HLDBNK") > 0 )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fabLotNumber  );
                           if( fabSortLot.getQtyIn() != null )
                           {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + result.getInt("CP_HLDBNK") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( result.getInt("CP_HLDBNK")+"" );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( result.getInt("CP_HLDBNK")+"" );
                            }
                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", CP_HLDBNK") ;
                            }
                            else
                            {
                                fabSortLot.setStage("CP_HLDBNK");
                            }
                        }
                        if( result.getInt("WQ")  > 0 )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fabLotNumber  );
                           if( fabSortLot.getQtyIn() != null )
                           {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + result.getInt("WQ") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( result.getInt("WQ")+"" );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( result.getInt("WQ")+"" );
                            }
                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", WQ") ;
                            }
                            else
                            {
                                fabSortLot.setStage("WQ");
                            }
                        }
                        if( result.getInt("WP") > 0 )
                        {
                           fabSortLot = (FabSortLot) m_sortDataMap.get( partNumber + fabLotNumber  );
                           if( fabSortLot.getQtyIn() != null )
                           {
                                try
                                {
                                    fabSortLot.setQtyIn( ( Integer.parseInt( fabSortLot.getQtyIn() ) + result.getInt("WP") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    fabSortLot.setQtyIn( result.getInt("WP") +"" );
                                }
                            }
                            else
                            {
                                fabSortLot.setQtyIn( result.getInt("WP")+"" );
                            }
                            if ( fabSortLot.getStage() != null )
                            {
                                fabSortLot.setStage ( fabSortLot.getStage() +", WP") ;
                            }
                            else
                            {
                                fabSortLot.setStage("WP");
                            }
                        }

                        if( fabSortLot.getQtyIn() != null && !"0".equalsIgnoreCase( fabSortLot.getQtyIn() ) )
                        {
                            fabSortLot.setCustProduct( partNumber );
                            fabSortLot.setLotId( fabLotNumber );
                            fabSortLot.setFabLotNumber( fabLotNumber );

                            if (result.getInt("CP_HLDBNK") > 0 || result.getInt("CP_HOLD") > 0 )
                            {
                                fabSortLot.setLotStatus( LOT_STATUS_HOLD );
                            }
                            else
                            {
                                fabSortLot.setLotStatus( LOT_STATUS_RUNNING );
                            }
                            if ( result.getString("LOTTYPE") != null && "Engineering".equalsIgnoreCase(result.getString("LOTTYPE")) )
                            {
                                fabSortLot.setLotCategory( LOT_CATEGORY_TYPE_ENGG );
                            }
                            else
                            {
                                fabSortLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                            }
                        }
                    }

                    if ( result.getInt("ASS_UNISS") > 0  && (result.getString("ASE_SCH") == null || "-".equalsIgnoreCase(result.getString("ASE_SCH") ) ) )
                    {
                        //System.out.println("lotNumber = " + lotNumber);
                        inventoryLot = (InventoryLot)m_dieBankDataMap.get( partNumber + fabLotNumber );

                        if ( inventoryLot == null )
                        {
                            inventoryLot = new InventoryLot( VENDOR_NAME_ASEK, LOCATION_TYPE_DIE_BANK );
                            m_dieBankDataMap.put( partNumber + fabLotNumber, inventoryLot );
                        }

                        int quantity = 0;
                        if (inventoryLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(inventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            quantity += result.getInt("ASS_UNISS");

                        }
                        else
                        {
                            quantity = result.getInt("ASS_UNISS");
                        }
                        if (quantity > 0 )
                        {
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setLotNumber( fabLotNumber );
                            inventoryLot.setQtyIn(quantity + "");
                            if ( result.getString("ASSY_RECDATE") != null && result.getString("ASSY_RECDATE").trim().length() > 5 )
                            {
                                inventoryLot.setDateIn( StringUtils.formatDate(result.getString("ASSY_RECDATE"), "yyyyMMdd","MM/dd/yyyy" ) );
                            }
                            inventoryLot.setFabLotNumber( fabLotNumber );
                            inventoryLot.setPkgType( result.getString("PKG_TYPE") );
                            inventoryLot.setLeadCount( result.getString("LEAD_CNT"));
                            inventoryLot.setStatus( LOT_STATUS_RUNNING );
                            inventoryLot.setLotCategory( LOT_CATEGORY_TYPE_PROD );
                        }

                    }//End Of Die Bank

                    //ASSY WIP
                    if (  (result.getInt("ASS_UNISS") > 0  && result.getString("ASE_SCH") != null && result.getString("ASE_SCH").length() > 3 ) || result.getInt("DSAW") > 0 || result.getInt("DA") > 0 || result.getInt("WB") > 0
                        || result.getInt("MD") > 0  || result.getInt("DT") > 0 || result.getInt("PP") > 0 || result.getInt("SM") > 0 || result.getInt("SF") > 0
                        || result.getInt("FVI") > 0 || result.getInt("PACKING") > 0 )
                    {
                         assemblyLot = (AssemblyLot)m_assemblyDataMap.get( partNumber + lotNumber + fabLotNumber );

                         if (assemblyLot == null)
                         {
                             assemblyLot = new AssemblyLot( VENDOR_NAME_ASEK, WIP_TYPE_ASSEMBLY );
                             m_assemblyDataMap.put( partNumber + lotNumber + fabLotNumber, assemblyLot);
                         }
                         if (result.getInt("ASS_UNISS") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("ASS_UNISS") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("ASS_UNISS")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("ASS_UNISS")+"");
                            }

                            if (assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", ASSY UNISS");
                            }
                            else
                            {
                                assemblyLot.setStage("ASSY UNISS");
                            }
                        }
                        if (result.getInt("DSAW") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("DSAW") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("DSAW")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("DSAW")+"");
                            }

                            if (assemblyLot.getStage() != null )
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Sawing");
                            }
                            else
                            {
                                assemblyLot.setStage("Sawing");
                            }
                        }
                        if (result.getInt("DA") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("DA") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("DA")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("DA")+"");
                            }

                            if (assemblyLot.getStage() != null)
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Die Attach");
                            }
                            else
                            {
                                assemblyLot.setStage("Die Attach");
                            }
                        }

                        if (result.getInt("WB") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("WB") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("WB") + "");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("WB")+"");
                            }

                            if (assemblyLot.getStage() != null)
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Wire Bond");
                            }
                            else
                            {
                                assemblyLot.setStage("Wire Bond");
                            }
                        }
                        if (  result.getInt("MD") > 0   )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("MD") +"") + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("MD")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("MD")+"");
                            }

                            if (assemblyLot.getStage() != null)
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Molding");
                            }
                            else
                            {
                                assemblyLot.setStage("Molding");
                            }
                        }

                        if (result.getInt("DT") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("DT")) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("DT")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("DT") +"");
                            }

                            if (assemblyLot.getStage() != null)
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Dejunk Trim");
                            }
                            else
                            {
                                assemblyLot.setStage("Dejunk Trim");
                            }
                        }
                        if (result.getInt("PP") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("PP") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("PP") +"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("PP")+"");
                            }

                            if (assemblyLot.getStage() != null)
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Plating");
                            }
                            else
                            {
                                assemblyLot.setStage("Plating");
                            }
                        }

                        if (result.getInt("SM") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("SM") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("SM")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("SM")+"");
                            }

                            if (assemblyLot.getStage() != null)
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", SM");
                            }
                            else
                            {
                                assemblyLot.setStage("SM");
                            }
                        }

                        if ( result.getInt("SF") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("SF") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("SF")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("SF")+"");
                            }

                            if (assemblyLot.getStage() != null)
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", SF");
                            }
                            else
                            {
                                assemblyLot.setStage("SF");
                            }
                        }
                        if (result.getInt("FVI") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("FVI") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("FVI")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("FVI")+"");
                            }

                            if (assemblyLot.getStage() != null)
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", Final Visual Insp");
                            }
                            else
                            {
                                assemblyLot.setStage("Final Visual Insp");
                            }
                        }
                        if ( result.getInt("PACKING") > 0 )
                        {
                            assemblyLot = (AssemblyLot) m_assemblyDataMap.get(partNumber + lotNumber + fabLotNumber);

                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    assemblyLot.setQtyIn((Integer.parseInt(assemblyLot.getQtyIn()) + result.getInt("PACKING") ) + "");
                                }
                                catch (Exception e)
                                {
                                    assemblyLot.setQtyIn(result.getInt("PACKING")+"");
                                }
                            }
                            else
                            {
                                assemblyLot.setQtyIn(result.getInt("PACKING")+"");
                            }

                            if (assemblyLot.getStage() != null)
                            {
                                assemblyLot.setStage(assemblyLot.getStage() + ", PACKING");
                            }
                            else
                            {
                                assemblyLot.setStage("PACKING");
                            }
                        }


                        if ( assemblyLot.getQtyIn() != null && !"0".equalsIgnoreCase( assemblyLot.getQtyIn() ))
                        {
                            assemblyLot.setDeviceName(partNumber);
                            assemblyLot.setLotNumber(lotNumber);
                            assemblyLot.setFabLotNumber(fabLotNumber);
                            if ( result.getString("HOLD_REAS") != null && result.getString("HOLD_REAS").trim().length() > 2 )
                            {
                                assemblyLot.setStatus(LOT_STATUS_HOLD);
                            }
                            else
                            {
                                assemblyLot.setStatus(LOT_STATUS_RUNNING);
                            }
                            assemblyLot.setPackageType(result.getString("PKG_TYPE"));
                            assemblyLot.setLeadCount( result.getString("LEAD_CNT"));
                            assemblyLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            if ( result.getString("ASSY_ISSDATE") != null && result.getString("ASSY_ISSDATE").trim().length() > 5 )
                            {
                                assemblyLot.setDateIn( StringUtils.formatDate(result.getString("ASSY_ISSDATE"), "yyyyMMdd","MM/dd/yyyy" )  );
                            }
                            else if ( result.getString("ASSY_RECDATE") != null && result.getString("ASSY_RECDATE").trim().length() > 5 )
                            {
                                assemblyLot.setDateIn( StringUtils.formatDate(result.getString("ASSY_RECDATE"), "yyyyMMdd","MM/dd/yyyy" ) );
                            }

                            if ( result.getString("ASSY_ORG_SOD") != null && result.getString("ASSY_ORG_SOD").trim().length() > 5  )
                            {
                                assemblyLot.setExpectedDateOut( StringUtils.formatDate(result.getString("ASSY_ORG_SOD"), "yyyyMMdd","MM/dd/yyyy" )  );
                            }

                         }
                   }//END OF ASSY

                    //TEST WIP
                    if ( result.getInt("FT_INTRA") > 0 || result.getInt("FT_UNISS") > 0 || result.getInt("BI") > 0 || result.getInt("FT1") > 0
                          || result.getInt("FT2") > 0  || result.getInt("FTX") > 0 || result.getInt("TM") > 0 || result.getInt("VM") > 0
                          || result.getInt("BK") > 0 || result.getInt("TR") > 0 || result.getInt("PK") > 0  )
                    {
                        testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                        if (testLot == null)
                        {
                            testLot = new TestLot(VENDOR_NAME_ASET);
                            m_ftDataMap.put(partNumber + lotNumber + fabLotNumber, testLot);
                        }

                       // FOR SETTING THE QTY IN
                       if( result.getInt("FT_INTRA") > 0  )
                       {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("FT_INTRA") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("FT_INTRA")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("FT_INTRA") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", FT_INTRA");
                            }
                            else
                            {
                                testLot.setStage("FT_INTRA");
                            }
                        }

                        if( result.getInt("FT_UNISS") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("FT_UNISS") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("FT_UNISS")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("FT_UNISS") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", FT_UNISS");
                            }
                            else
                            {
                                testLot.setStage("FT_UNISS");
                            }
                        }
                        if( result.getInt("BI") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("BI") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("BI")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("BI") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", BI");
                            }
                            else
                            {
                                testLot.setStage("BI");
                            }
                        }
                        if( result.getInt("FT1") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("FT1") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("FT1")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("FT1") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", FT1");
                            }
                            else
                            {
                                testLot.setStage("FT1");
                            }
                        }
                        if( result.getInt("FT2") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("FT2") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("FT2")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("FT2") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", FT2");
                            }
                            else
                            {
                                testLot.setStage("FT2");
                            }
                        }

                        if( result.getInt("FTX") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("FTX") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("FTX")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("FTX") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", FTX");
                            }
                            else
                            {
                                testLot.setStage("FTX");
                            }
                        }
                        if( result.getInt("TM") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("TM") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("TM")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("TM") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", TM");
                            }
                            else
                            {
                                testLot.setStage("TM");
                            }
                        }
                        if( result.getInt("VM") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("VM") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("VM")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("VM") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", VM");
                            }
                            else
                            {
                                testLot.setStage("VM");
                            }
                        }
                        if( result.getInt("BK") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("BK") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("BK")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("BK") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", BK");
                            }
                            else
                            {
                                testLot.setStage("BK");
                            }
                        }
                        if( result.getInt("TR") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("TR") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("TR")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("TR") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", TR");
                            }
                            else
                            {
                                testLot.setStage("TR");
                            }
                        }
                        if( result.getInt("PK") > 0  )
                        {
                           testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber );
                           if( testLot.getQtyIn() != null )
                           {
                                try
                                {
                                    testLot.setQtyIn( ( Integer.parseInt( testLot.getQtyIn() ) + result.getInt("PK") ) + "" );
                                }
                                catch(Exception e)
                                {
                                    testLot.setQtyIn( result.getInt("PK")+"" );
                                }
                            }
                            else
                            {
                                testLot.setQtyIn( result.getInt("PK") + "" );
                            }
                            if ( testLot.getStage() != null )
                            {
                                testLot.setStage( testLot.getStage() + ", PK");
                            }
                            else
                            {
                                testLot.setStage("PK");
                            }
                        }

                        if ( testLot.getQtyIn() != null && !"0".equalsIgnoreCase(testLot.getQtyIn() ) )
                        {
                            testLot.setPartNumber( partNumber );
                            testLot.setLotNumber( lotNumber );
                            testLot.setFabLotNumber( fabLotNumber );
                            if ( result.getString("FT_ISSDATE") != null && result.getString("FT_ISSDATE").trim().length() > 5 )
                            {
                                testLot.setDateIn( StringUtils.formatDate(result.getString("FT_ISSDATE"), "yyyyMMddHHmmss","MM/dd/yyyy" )  );
                            }
                            else if ( result.getString("FT_RECDATE") != null && result.getString("FT_RECDATE").trim().length() > 5 )
                            {
                                testLot.setDateIn( StringUtils.formatDate(result.getString("FT_RECDATE"), "yyyyMMddHHmmss","MM/dd/yyyy" )  );
                            }

                            if (result.getString("COMMIT_DATE") != null && result.getString("COMMIT_DATE").trim().length() > 5 )
                            {
                                testLot.setExpecetedDate( StringUtils.formatDate(result.getString("COMMIT_DATE"), "yyyyMMdd","MM/dd/yyyy" )  );
                            }

                            testLot.setLeadCount( result.getString("LEAD_CNT") );
                            testLot.setPackageType( result.getString("PKG_TYPE") );
                            
                            if ( result.getString("HOLD_REAS") != null && result.getString("HOLD_REAS").trim().length() > 2 )
                            {
                               testLot.setStatus(LOT_STATUS_HOLD);
                            }
                            else
                            {
                               testLot.setStatus(LOT_STATUS_RUNNING);
                            }
                            testLot.setPsType( LOCATION_TYPE_FT );
                            testLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                       }
                    }//end of TEST WIP
                    
                    //FG WIP
                    if ( result.getInt("TDRP") > 0 )
                    {

                        inventoryLot = (InventoryLot) m_fgDataMap.get( partNumber + lotNumber + fabLotNumber);

                        if (inventoryLot == null)
                        {
                            inventoryLot = new InventoryLot(VENDOR_NAME_ASET, LOCATION_TYPE_FG );
                            m_fgDataMap.put( partNumber + lotNumber + fabLotNumber, inventoryLot);
                        }
                        int quantity = 0;
                        if (inventoryLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(inventoryLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            quantity += result.getInt("TDRP");
                        }
                        else
                        {
                           quantity = result.getInt("TDRP");
                        }

                        if ( quantity > 0 )
                        {
                            inventoryLot.setLotNumber( lotNumber );
                            inventoryLot.setPartNumber( partNumber );
                            inventoryLot.setStatus( LOT_STATUS_RUNNING );
                            inventoryLot.setFabLotNumber( fabLotNumber );
                            inventoryLot.setCategory( LOT_CATEGORY_TYPE_PROD );
                            if ( result.getString("FT_ISSDATE") != null && result.getString("FT_ISSDATE").trim().length() > 5 )
                            {
                                inventoryLot.setDateIn( StringUtils.formatDate(result.getString("FT_ISSDATE"), "yyyyMMddHHmmss", "MM/dd/yyyy"));
                            }
                            inventoryLot.setQtyIn( quantity +"");
                            inventoryLot.setPkgType( result.getString("PKG_TYPE") );
                            inventoryLot.setLeadCount( result.getString("LEAD_CNT") );
                        }
                    } //END OF FG
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Exception while parsing ASE TurnKey Wip Report " + e);
            throw  e ;
        }
    }
    private void parseAssyShipReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result     = null;
        ShippedLot shippedLot   = null;
        String lotNumber        = null;
        String partNumber       = null;
        String fabLotNumber     = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for Assy Ship Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for Assy Shipg Report..." );

             Parser parser = ParserFactory.getCSVParser( detailWipFile, ',' );

            ((CSVParserImpl)parser).setHeaderRowIdx(2);
            ((CSVParserImpl)parser).setDataStartRowIdx(3);

            result = parser.parse();

            while ( result.next() )
            {
                partNumber = result.getString("DEVICE");
                fabLotNumber = result.getString("CUST_LOT");
                lotNumber   = result.getString("ASE_SCH");
                String nextLocation = null;
                String shipDate = null;
                nextLocation = result.getString("SHIP_TO");
                if ( result.getString("SHIP_DATE") != null && result.getString("SHIP_DATE").trim().length() > 5 )
                {
                    shipDate = StringUtils.formatDate(result.getString("SHIP_DATE"), "yyyyMMdd", "MM/dd/yyyy");
                }
                if (partNumber!= null && partNumber.trim().length()>0 && lotNumber != null && lotNumber.trim().length() > 0 )
                {
                    shippedLot = ( ShippedLot ) m_assyShipDataMap.get ( partNumber + lotNumber + fabLotNumber + nextLocation + shipDate );

                   if ( shippedLot == null )
                   {
                        shippedLot = new ShippedLot(VENDOR_NAME_ASEK, LOCATION_TYPE_ASSEMBLY);
                        m_assyShipDataMap.put( partNumber + lotNumber + fabLotNumber + nextLocation + shipDate, shippedLot );
                   }

                   int quantity = 0;
                   if ( shippedLot.getShipQty() != null )
                   {
                       try
                       {
                           quantity = Integer.parseInt( shippedLot.getShipQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                       quantity += result.getInt("SHIP_QTY");

                   }
                   else
                   {
                      quantity = result.getInt("SHIP_QTY");
                   }

                   if ( quantity > 0 )
                   {
                       shippedLot.setLotNumber( lotNumber );
                       shippedLot.setPartNumber( partNumber );
                       shippedLot.setShipDate( shipDate );
                       shippedLot.setShipQty( quantity + "" );
                       shippedLot.setNextLocation( nextLocation );
                       shippedLot.setInvoiceNumber( result.getString("INV_NO") );
                       shippedLot.setFabLotNumber( fabLotNumber );
                       shippedLot.setMawb( result.getString("MAWB") );
                       shippedLot.setHawb( result.getString("HAWB") );
                       shippedLot.setEtd( StringUtils.formatDate( !"00000000".equalsIgnoreCase(result.getString("ETD")) ? result.getString("ETD"): null, "yyyyMMdd", "MM/dd/yyyy") );
                       shippedLot.setEta( StringUtils.formatDate( !"00000000".equalsIgnoreCase(result.getString("ETA")) ? result.getString("ETA"): null, "yyyyMMdd", "MM/dd/yyyy") );
                       shippedLot.setFlightNumber( result.getString("FLT") );
                       
                       shippedLot.setProcessType( LOCATION_TYPE_ASSEMBLY );
                   }

                }
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Exception while parsing ASEK Ship Report " + e);
            throw  e;
        }
    }
    private void parseTestShipReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result     = null;
        ShippedLot shippedLot   = null;
        String lotNumber        = null;
        String partNumber       = null;
        String fabLotNumber     = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for Test Ship Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for Test Ship Report..." );

             Parser parser = ParserFactory.getCSVParser( detailWipFile, ',' );

            ((CSVParserImpl)parser).setHeaderRowIdx(2);
            ((CSVParserImpl)parser).setDataStartRowIdx(3);

            result = parser.parse();

            while ( result.next() )
            {
                partNumber = result.getString("SOURCE_DEVICE");
                fabLotNumber = result.getString("CUST_LOT");
                lotNumber   = result.getString("ASE_SCH");
                String nextLocation = null;
                String shipDate = null;
                nextLocation = result.getString("SHIP_TO");
                if ( result.getString("SHIP_DATE") != null && result.getString("SHIP_DATE").trim().length() > 5 )
                {
                    shipDate = StringUtils.formatDate(result.getString("SHIP_DATE"), "yyyyMMdd", "MM/dd/yyyy");
                }
                if (partNumber!= null && partNumber.trim().length()>0 && lotNumber != null && lotNumber.trim().length() > 0 )
                {
                    shippedLot = ( ShippedLot ) m_testShipDataMap.get ( partNumber + lotNumber + fabLotNumber + nextLocation + shipDate );

                   if ( shippedLot == null )
                   {
                        shippedLot = new ShippedLot(VENDOR_NAME_ASET, LOCATION_TYPE_TEST);
                        m_testShipDataMap.put( partNumber + lotNumber + fabLotNumber + nextLocation + shipDate, shippedLot );
                   }

                   int quantity = 0;
                   if ( shippedLot.getShipQty() != null )
                   {
                       try
                       {
                           quantity = Integer.parseInt( shippedLot.getShipQty() );
                       }
                       catch ( NumberFormatException ignore )
                       {
                           quantity = 0;
                       }
                       quantity += result.getInt("GOOD_QTY");

                   }
                   else
                   {
                      quantity = result.getInt("GOOD_QTY");
                   }

                   if ( quantity > 0 )
                   {
                       shippedLot.setLotNumber( lotNumber );
                       shippedLot.setPartNumber( partNumber );
                       shippedLot.setShipDate( shipDate );
                       shippedLot.setShipQty( quantity + "" );
                       shippedLot.setNextLocation( nextLocation );
                       shippedLot.setInvoiceNumber( result.getString("INV_NO") );
                       shippedLot.setFabLotNumber( fabLotNumber );
                       shippedLot.setMawb( result.getString("MAWB") );
                       shippedLot.setHawb( result.getString("HAWB") );
                       shippedLot.setEtd( StringUtils.formatDate( !"00000000".equalsIgnoreCase(result.getString("ETD")) ? result.getString("ETD"): null, "yyyyMMdd", "MM/dd/yyyy") );
                       shippedLot.setEta( StringUtils.formatDate( !"00000000".equalsIgnoreCase(result.getString("ETA")) ? result.getString("ETA"): null, "yyyyMMdd", "MM/dd/yyyy") );
                       shippedLot.setFlightNumber( result.getString("FLT") );
                       shippedLot.setProcessType( LOCATION_TYPE_FT );
                   }

                }
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Exception while parsing ASET CP Yiled Report " + e);
            throw e;
        }
    }
    private void parseAssyYieldReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );

        ParserResult result     = null;
        FabSortLot  sortBinLot  = null;
        FabSortLot  sortBinTotalLot  = null;
        String lotNumber        = null;
        String partNumber       = null;
        int waferId             = 0;
        HashMap waferIdMap = new HashMap();
        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for CP Binning Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for CP Binning Report..." );

             Parser parser = ParserFactory.getCSVParser( detailWipFile, ',' );

            ((CSVParserImpl)parser).setHeaderRowIdx(2);
            ((CSVParserImpl)parser).setDataStartRowIdx(3);

            result = parser.parse();

            while ( result.next() )
            {
                partNumber = result.getString("DEVICE");
                lotNumber = result.getString("CUST_LOT");
                waferId   = result.getInt("WAFER_ID");

                if (partNumber!= null && partNumber.trim().length()>0 && lotNumber != null && lotNumber.trim().length() > 0 && waferId > 0 && waferId < 26 )
                {

                }
            }

        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.println("Exception while parsing ASEK Assy Yiled Report " + e);
            throw e;
        }
    }

    private ArrayList<TestLotBinReport> parseFTBinReport( String fileName ) throws Exception
    {
        if ( fileName == null ) throw new ScalarException( "Filename argument is null" );
        ParserResult result     = null;

        String lotNumber        = null;
        String partNumber       = null;
        String fablotNumber     = null;
        ArrayList<TestLotBinReport> ftYieldList  = new ArrayList<TestLotBinReport>();
        String line                     = null;

        try
        {
            File detailWipFile = new File( fileName );
            if ( detailWipFile.exists() == false )
            {
                throw new ScalarException( fileName + " doesnt exist" );
            }

            log( "\nFound " + fileName );
            log( "\nParsing the file for Ft Yield Report..." );
            System.out.println( "\nFound " + fileName );
            System.out.println( "\nParsing the file for FT Yield Report..." );

            Parser parser = ParserFactory.getCSVParser( detailWipFile, ',' );

            ((CSVParserImpl)parser).setHeaderRowIdx(2);
            ((CSVParserImpl)parser).setDataStartRowIdx(3);

            result = parser.parse();

            while ( result.next() )
            {
                partNumber = result.getString("Device");
                fablotNumber = result.getString("CUST_LOT");
                lotNumber   =  result.getString("ASE_SCH");
                if ( lotNumber == null)
                {
                    lotNumber = fablotNumber;
                }

                if ( partNumber != null && partNumber.length() > 0 && lotNumber != null && lotNumber.length() > 0 )
                {
                    TestLotBinReport ftYieldStatus = new TestLotBinReport();

                    ftYieldStatus.setDeviceName( partNumber );
                    ftYieldStatus.setLotNumber( lotNumber );
                    ftYieldStatus.setCustLotNo( fablotNumber );
                    //ftYieldStatus.setPoNumber( result.getString("Release No"));
                    ftYieldStatus.setPkgType( result.getString("PKG_TYPE") );
                    ftYieldStatus.setLeadCount( result.getString("LEAD_CNT") );
                    ftYieldStatus.setStage( result.getString("RECPID") ); // Insertion
                    ftYieldStatus.setTestProg(result.getString("TEST_PROG"));
                    ftYieldStatus.setTesterId( result.getString("TESTER") );
                    //ftYieldStatus.setDateCode( result.getString("Trace Code") );
                    ftYieldStatus.setHandler( result.getString("HANDLR") );
                    ftYieldStatus.setLb(result.getString("LO_BRD"));
                    ftYieldStatus.setDateIn( StringUtils.formatDate( result.getString("TEST_IN_DATE"), "yyyyMMdd", "MM/dd/yyyy" ) );
                    ftYieldStatus.setDateOut(StringUtils.formatDate( result.getString("TEST_OUT_DATE"), "yyyyMMdd", "MM/dd/yyyy" ));
                    ftYieldStatus.setInQty( result.getInt("IN_QTY") +"");
                    ftYieldStatus.setPassQty( result.getInt("GOOD_QTY")+"" );
                    //ftYieldStatus.setDamageQty( result.getString("Defect Qty") );
                    ftYieldStatus.setYield( result.getString("YIELD").replaceAll("%",""));
                    ftYieldStatus.setBin1( result.getString("BIN1") );
                    ftYieldStatus.setBin2(result.getString("BIN2"));
                    ftYieldStatus.setBin3(result.getString("BIN3"));
                    ftYieldStatus.setBin4(result.getString("BIN4"));
                    ftYieldStatus.setBin5(result.getString("BIN5"));
                    ftYieldStatus.setBin6(result.getString("BIN6"));
                    ftYieldStatus.setBin7(result.getString("BIN7"));
                    ftYieldStatus.setBin8(result.getString("BIN8"));
                    ftYieldStatus.setBin9(result.getString("BIN9"));
                    ftYieldStatus.setBin10(result.getString("BIN10"));
                    ftYieldStatus.setLossQty( result.getString("LOSS") );
                    ftYieldStatus.setVmQty( result.getString("VM") );
                    ftYieldStatus.setOsQty( result.getString("OS_QTY") );

                    ftYieldList.add( ftYieldStatus );

                }
            }
        }
        catch ( Exception ex )
        {
            System.out.println("ERROR!!!ERROR!!! While Parsing the ASET FT Yield Report");
            ex.printStackTrace();
            throw ex;
        }

        return ftYieldList;
    }
    private void log( String message )
    {
        if ( message == null ) return;

        if ( m_parserLogger != null )
        {
            m_parserLogger.logDebug( message + LINE_SEPARATOR );
        }
        else
        {
            System.out.println( message );
        }
    } // end of log method
}
